//
//  SharedBarView.m
//  KYProject
//
//  Created by Mxxx on 2016/12/22.
//  Copyright © 2016年 Roman. All rights reserved.
//

#import "SharedBarView.h"
#import "DefineConstant.h"

@interface SharedBarView ()

@property (nonatomic,weak)IBOutlet UIImageView *iv_WechatLogo;
@property (nonatomic,weak)IBOutlet UIImageView *iv_sinaLogo;

@property (nonatomic,weak)UIWebView *webView;
@property (nonatomic,weak)UIToolbar *toolBar;
@end
@implementation SharedBarView


+ (instancetype)barView
{
    return [[NSBundle mainBundle]loadNibNamed:@"SharedBarView" owner:nil options:nil].lastObject;
}

- (void)awakeFromNib
{
    [super awakeFromNib];
    
    //手势
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
    UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap)];
    
    [_iv_WechatLogo addGestureRecognizer:tap1];
    [_iv_sinaLogo addGestureRecognizer:tap2];
    
    _iv_WechatLogo.tag = 1;
    _iv_sinaLogo.tag = 2;
}

#pragma mark - 手势监听
- (void)tap:(UITapGestureRecognizer *)tap
{
    //url
    NSURL *url = [[NSURL alloc]init];
    
    //点击了哪个logo
    
    if (tap.view.tag == 1) {
        url = [NSURL URLWithString:@"http://weixin.qq.com/cgi-bin/readtemplate?check=false&t=w_indexn73&uin=&stype=&promote=1003799&fr=&"];
    }
    else if (tap.view.tag == 2)
    {
        url = [NSURL URLWithString:@"http://tech.sina.com.cn/z/sinawap/"];
    }
    //创建webview
    UIWebView *web = [[UIWebView alloc]init];
    [KeyWindow.rootViewController.view addSubview:web];
    
    web.frame = CGRectMake(0, 64, KeyWindow.width, KeyWindow.height - 64);
    
    [web loadRequest:[NSURLRequest requestWithURL:url]];
    
    //webView顶部toolbar
    UIToolbar *toolBar = [[UIToolbar alloc]init];
    [KeyWindow.rootViewController.view addSubview:toolBar];
    
    toolBar.frame = CGRectMake(0, 0, KeyWindow.width, 64);
    toolBar.backgroundColor = [UIColor colorWithRed:0.976 green:0.976 blue:0.976 alpha:1.00];
    
    //添加返回按钮
    UIBarButtonItem *backBar = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(goBack)];
    toolBar.items = @[backBar];
    _webView = web;
    _toolBar = toolBar;
}

//返回按钮监听
- (void)goBack
{
    NSLog(@"back");
    [_webView removeFromSuperview];
    [_toolBar removeFromSuperview];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
